package report

import (
	"fmt"
	"time"

	"gddgame.cc/galaxy/binocle/module/report"
	"gddgame.cc/galaxy/utils/orm"
)

type ReportStore interface {
}

var (
	ServerReportModel = serverReportModel{
		orm.NewModel("server_report", &ServerReport{}, nil),
	}
)

type serverReportModel struct {
	*orm.Model
}

func (model *serverReportModel) GetReports(server string, metric string, startTime *time.Time, endTime *time.Time) []report.MetricReport {
	var list []ServerReport
	if _, err := model.Query().
		Where(">=", "create_time", startTime).
		Where("<=", "create_time", endTime).
		Where("=", "server", server).
		Where("=", "metric", metric).Find(&list); err != nil {
		return []report.MetricReport{}
	}
	reports := make([]report.MetricReport, len(list))
	for key, r := range list {
		reports[key] = report.MetricReport{
			Value: r.Value,
			Time:  r.CreateTime,
		}
	}
	return reports
}
func (model *serverReportModel) SaveReports(server string, metric string, value float32, t *time.Time) {
	if _, err := model.Insert(&ServerReport{
		Server:     server,
		Metric:     metric,
		Value:      value,
		CreateTime: t,
	}); err != nil {
		fmt.Println(err)
	}
}

type ServerReport struct {
	Id     int     `json:"id" orm:"auto" sql:"INT"`
	Server string  `json:"server" sql:"VARCHAR"`
	Metric string  `json:"metric" sql:"VARCHAR"`
	Value  float32 `json:"value" sql:"Float"`

	CreateTime *time.Time `json:"create_time" sql:"DATETIME"`
}
